/**
 * crawler
 *
 * outlook@China software studio
 * Copyright (c) 2008-2010. All Rights Reserved.
 * http://www.outlook.com
 */

package org.outlook.crawler.util.parser;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang.StringUtils;
import org.outlook.common.data.KeyValue;

/**
 * function:
 * @author Watson email: watson.wu@hotmail.com
 * @date 2011
 */
public class ELParser {
	
	private static final String CURRENT_DATE = "CURRENT_DATE";
	private static final String CURRENT_TIME = "CURRENT_TIME";
	private static final String CURRENT_DATETIME = "CURRENT_DATETIME";
	public static final String SPLITER = "${;}";
	public static final String KEYWORD = "${keyword}";

	private static String[] split(String string) {
		return null;
	}

	public static Cookie[] parserCookies(String string) {
		if(StringUtils.isEmpty(string)) return null;
		String[] expressions = split(string);
		KeyValue[] pairs = parser(expressions);
		Cookie[] array = new Cookie[pairs.length];
		int i = 0;
		for(KeyValue keyValue : pairs) {
			final Cookie cookie = new Cookie();
			array[i++] = cookie;
			cookie.setName(keyValue.getKey());
			cookie.setValue(keyValue.getValue() == null? null : (String)keyValue.getValue());
		}
		return array;
	}

	public static NameValuePair[] parserParameters(String string) {
		if(StringUtils.isEmpty(string)) return null;
		String[] expressions = split(string);
		KeyValue[] pairs = parser(expressions);
		NameValuePair[] array = new NameValuePair[pairs.length];
		int i = 0;
		for(KeyValue keyValue : pairs) {
			final Cookie cookie = new Cookie();
			array[i++] = cookie;
			cookie.setName(keyValue.getKey());
			cookie.setValue(keyValue.getValue() == null? null : (String)keyValue.getValue());
		}
		return array;
	}

	public static KeyValue[] parser(String[] expressions) {
		if(expressions == null) return null;
		KeyValue[] pairs = new KeyValue[expressions.length];
		int i = 0;
		for(String str : expressions) {
			pairs[i++] = parser(str);
		}
		return pairs;
	}

	public static KeyValue parser(String expression) {
		//TODO
		return null;
	}

	public static String parserURL(String api, String keyword) {
		api = api.replaceAll(KEYWORD, keyword);
//		api = api.replaceAll(CURRENT_DATE, replacement);
		return null;
	}

}
